/* Copyright Andy Mitchell 2007. No warranties */

function Environment(gtdinboxPage, elHeader){
try{
	this.gtdinboxPage = gtdinboxPage;
	this.doc = this.gtdinboxPage.doc;
	this._eventObservers = new EventObservers(this);
	
	this._labelsRenamed = [];
	this._labelsRemoved = [];
	this._eventObservers.observe( elHeader.ownerDocument.body, "mousedown", this.mouseDown, true );
	this.elAlertBox = this.gtdinboxPage.gmailDOM.element("alert-box");
	this._eventObservers.observe( this.elAlertBox, "DOMNodeInserted", this.alertBoxChange, false );
	
	this.emails = {};
	this.email = this.gtdinboxPage.gmailDOM.local("email-address");
	this.emails[this.email] = true;
	
	
}catch(e){
	if( GTDDEBUGMODE ) alert("Environment Contruct\n" + e.toString());
}
}

Environment.prototype.parseAjaxResponse = function(ajaxArray){
	if( ajaxArray ){
		for( var i = 0; i < ajaxArray.length; i++ ){
			if( ajaxArray[i][0]=="ugn" ){
				this.name = ajaxArray[i][1];
			}
			if( ajaxArray[i][0]=="cfs" ){
				var addresses = ajaxArray[i][1];
				for( var j = 0; j < addresses.length; j++ ){
					if( !this.emails[addresses[j][1]] ) this.emails[addresses[j][1]] = addresses[j][0];
				}
			}
		}
	}
};


Environment.prototype.mouseDown = function(event){
	var el = GIPROTOTYPE.element(event);
	if( event.button!=0 ) return;
	
	if( el.getAttribute("act")=="rename" && el.nodeName=="SPAN" ){	// clicked a rename
		while(el.parentNode && el.nodeName!="TR") el = el.parentNode;
		if( el.nodeName=="TR" ){
			el = el.getElementsByTagName("TD")[0].getElementsByTagName("SPAN")[0];
			if( el.className=="f8w49d" ){
				this._labelsRenamed.push(el.innerHTML);
			}
		}
	}else if( el.getAttribute("act")=="remove" ){
		while(el.parentNode && el.nodeName!="TR") el = el.parentNode;
		if( el.nodeName=="TR" ){
			el = el.getElementsByTagName("TD")[0].getElementsByTagName("SPAN")[0];
			if( el.className=="f8w49d" ){
				this._labelsRemoved.push(el.innerHTML);
			}
		}
	}
};
Environment.prototype.alertBoxChange = function(event){
	// Test to see if an outstanding renamed-label has been confirmed
try{
	for( var i = 0; i < this._labelsRenamed.length; i++ ){ // Renamed
		var re = new RegExp("\"" + this._labelsRenamed[i].replace(/([^\w\d\s\t\n])/g, "\\$1") + "\".+\"(.+)\"");
		if( re.test(this.elAlertBox.innerHTML) ){
			var match = this.elAlertBox.innerHTML.match(re);
			if( match[1] ){
				this.gtdinboxPage.sp.updateLabelName(this._labelsRenamed[i], match[1]);
				this._labelsRenamed.splice(i,1);
				break;
			}
		}
	}
	
	for( var i = 0; i < this._labelsRemoved.length; i++ ){ // Renamed
		var re = new RegExp("\"" + this._labelsRemoved[i].replace(/([^\w\d\s\t\n])/g, "\\$1") + "\"");
		if( re.test(this.elAlertBox.innerHTML) ){
			this.gtdinboxPage.sp.updateLabelName( this._labelsRemoved[i], "gtd.deleted." + this._labelsRemoved[i] );
			this._labelsRemoved.splice(i,1);
			break;
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("environment alertBoxChange\n" + e.toString());
}
};

Environment.prototype.destroy = function(){
	this._eventObservers.destroy();
	this.gtdinboxPage = null;
	this.doc = null;
};